﻿using System;
using System.Diagnostics;
using X25519;

namespace Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            // 测试次数
            const int tests = 1000;
            Stopwatch stopwatch = new Stopwatch();
            // 简单密钥协议
            {
                stopwatch.Start();
                // 生成密钥
                var k1 = X25519KeyAgreement.GenerateKeyPair();
                var k2 = X25519KeyAgreement.GenerateKeyPair();

                // 产生共享秘密
                var shared1 = X25519KeyAgreement.Agreement(k1.PrivateKey, k2.PublicKey);
                var shared2 = X25519KeyAgreement.Agreement(k2.PrivateKey, k1.PublicKey);
                stopwatch.Stop();
                // 打印它们以确保它们相同
                Console.WriteLine(Convert.ToBase64String(shared1));
                Console.WriteLine(Convert.ToBase64String(shared2));
                Console.WriteLine($"已完成完整的密钥协议{stopwatch.Elapsed}");
            }
            stopwatch.Reset();
            // 基准密钥生成
            {
                X25519KeyPair key;
                stopwatch.Start();
                for (int i = 0; i < tests; i++)
                    key = X25519KeyAgreement.GenerateKeyPair();
                stopwatch.Stop();
                Console.WriteLine($"密钥生成在{stopwatch.Elapsed}中完成。 " +
                                  $"{Math.Round((double)tests/stopwatch.Elapsed.Milliseconds * 1000)} 项/秒");
            }
            stopwatch.Reset();
            //基准协议
            {
                X25519KeyPair[] alice = new X25519KeyPair[tests];
                X25519KeyPair[] bob = new X25519KeyPair[tests];
                // 我想基准测试关键协议而不是生成密钥
                for (int i = 0; i < tests; i++)
                {
                    alice[i] = X25519KeyAgreement.GenerateKeyPair();
                    bob[i] = X25519KeyAgreement.GenerateKeyPair();
                }
                stopwatch.Start();
                for (int i = 0; i < tests; i++)
                    X25519KeyAgreement.Agreement(alice[i].PrivateKey, bob[i].PublicKey);
                stopwatch.Stop();
                Console.WriteLine($"在{stopwatch.Elapsed}中达成的关键协议" +
                                  $"{Math.Round((double)tests/stopwatch.Elapsed.Milliseconds * 1000)} 参数/秒");
                stopwatch.Reset();
            }
        }
    }
}
